//
//  NSString+Inflection.m
//  My Wine
//
//  Created by Johnnie Walker on 15/08/2008.
//  Copyright 2008 Random Sequence. All rights reserved.
//

#import "NSString+Inflection.h"
#import "RegexKitLite.h"

@implementation NSString (Inflection)

/*
 // Test cases
 
 NSLog(@"[@\"my_wine_label\" classifiedString]): %@",[@"my_wine_label" classifiedString]);
 NSLog(@"[@\"myWineLabel\" tableizedString]): %@",[@"myWineLabel" tableizedString]);
 NSLog(@"[@\"wine_label\" classifiedString]): %@",[@"wine_label" classifiedString]);
 NSLog(@"[@\"WineLabel\" tableizedString]): %@",[@"WineLabel" tableizedString]);
 NSLog(@"[@\"wine\" classifiedString]): %@",[@"wine" classifiedString]);
 NSLog(@"[@\"Wine\" tableizedString]): %@",[@"Wine" tableizedString]);	 
 
 */

- (NSString *)tableizedString {
	return [[self stringByReplacingOccurrencesOfRegex:@"([a-z])([A-Z])" withString:@"$1_$2"] lowercaseString];
}

- (NSString *)classifiedString {
	NSArray *parts = [self componentsSeparatedByString:@"_"];
	NSMutableArray *capitalizedParts = [NSMutableArray arrayWithCapacity:[parts count]];	
	for (NSString *part in parts) {
		[capitalizedParts addObject:[part capitalizedString]];
	}
	// [capitalizedParts replaceObjectAtIndex:0 withObject:[[capitalizedParts objectAtIndex:0] lowercaseString]];
	return [capitalizedParts componentsJoinedByString:@""];
}

@end
